package com.codeshellme.bookmanager.service.impl;

import com.codeshellme.bookmanager.dao.SysDao;
import com.codeshellme.bookmanager.dto.BookAuthor;
import com.codeshellme.bookmanager.dto.BookItem;
import com.codeshellme.bookmanager.dto.UmsUser;
import com.codeshellme.bookmanager.service.SysService;
import com.codeshellme.bookmanager.utils.SimpleCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SysServiceImpl implements SysService {

    @Autowired
    private SysDao sysDao;

    @Override
    public PageInfo<Map<String, Object>> querySysDict(
            Integer dictId, Integer fatherId,
            Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> infos = sysDao.querySysDict(dictId, fatherId);
        return new PageInfo<>(infos);
    }

    @Override
    public PageInfo<Map<String, Object>> queryAllAuthors(
            String name, Byte sex, Integer nationality_id, Byte status,
            String date_start, String date_end,
            Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> authors = sysDao.queryAllAuthors(
                name, sex, nationality_id, status, date_start, date_end);
        return new PageInfo<>(authors);
    }

    @Override
    public PageInfo<Map<String, Object>> queryAllSysBooks(String name, Integer type_id, Integer author_id, Byte status,
                                                          String date_start, String date_end, String q,
                                                          Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> books = sysDao.queryAllSysBooks(
                name, type_id, author_id, status, date_start, date_end, q);
        return new PageInfo<>(books);
    }

    @Override
    public List<Map<String, Object>> queryAllNations(String q) {
        return sysDao.queryAllNations(q);
    }

    @Override
    public List<Map<String, Object>> queryBookTypes(String q) {
        return sysDao.queryBookTypes(q);
    }

    @Override
    public List<Map<String, Object>> queryBookAuthors(String q) {
        return sysDao.queryBookAuthors(q);
    }

    @Override
    public int updateAuthor(BookAuthor author) {
        String name = author.getName();
        if (name != null && !name.equals("")) {
            author.setPincode(SimpleCode.getFirstSpell(name));
        }

        return sysDao.updateAuthor(author);
    }

    @Override
    public int addAuthor(BookAuthor author) {
        String name = author.getName();
        if (name != null && !name.equals("")) {
            author.setPincode(SimpleCode.getFirstSpell(name));
        }

        return sysDao.addAuthor(author);
    }

    @Override
    public int deleteAuthorById(Integer authorId) {
        return sysDao.deleteAuthorById(authorId);
    }

    @Override
    public int updateBookItem(BookItem book) {
        String name = book.getName();
        if (name != null && !name.equals("")) {
            book.setPincode(SimpleCode.getFirstSpell(name));
        }

        return sysDao.updateBookItem(book);
    }

    @Override
    public int addBookItem(BookItem book) {
        String name = book.getName();
        if (name != null && !name.equals("")) {
            book.setPincode(SimpleCode.getFirstSpell(name));
        }

        return sysDao.addBookItem(book);
    }

    @Override
    public int deleteBookItemById(Integer bookId) {
        return sysDao.deleteBookItemById(bookId);
    }

    @Override
    public Boolean verifyBookItemName(String name) {
        Map<String, Object> book = sysDao.findBookItemByName(name);
        if (book == null) {
            return true;    // 没有找到书名，可用
        } else {
            return false;   // 找到了书名，不可用
        }
    }
}
